<?php

/*

  Action	$3

  +2 Cards
  Each player passes a card in their hand to the player on their left.
  You may trash a card from your hand.

 */

class Masquerade extends Card implements iAction, iKingdom, iIntrigue {

    public function __construct() {
        parent::__construct("Masquerade", 3);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+2 Cards",
            "Each player passes a card in their hand to the player on their left.  You may trash a card from your hand.");
    }
    
    public function play(Game $game, $isRealCard) {
        $drawed_cards = $game->drawCards(2);
         ScreenDisplay::addScreenMessage($game, true, "draw " . nicePrint($drawed_cards), "draws " . sizeof($drawed_cards) . " cards");

        $names = $game->getPlayersNames();
        foreach ($names as $name) {
            $playHandOther = getUniqueCards($game->getPlayHand($name));
            if (sizeof($playHandOther) > 0) {

                $nextPlayerName = $game->getNextPlayerName($name);

                $output = MySocket::GetUserReply($game, Input_ChooseOne, $playHandOther, 1, 1, null, "Choose a card to pass to " . $nextPlayerName, $name, true);
            }
            else {
                 ScreenDisplay::addScreenMessage($game, true, "pass nothing to $nextPlayerName", "passes nothing to $nextPlayerName");
            }
        }

        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {

            $card_name = $output[0];
            $nextPlayerName = $game->getNextPlayerName($name);

            //note that masquerade does not give a card as a 'gain' command, so no gainning here... watchtower and similar affects cant help.
            $game->removeCardFromPlayHand($card_name, $name);
            $game->putCardInPlayHand($card_name, $nextPlayerName);
             ScreenDisplay::addScreenMessage($game, true, "pass ".  nicePrint($card_name)." to $nextPlayerName", "passes ".ScreenDisplay::$LMSG." to $nextPlayerName", $name);
            $game->setPhase(Phase_Sleep, $name);
        }

        //waking this player up, he has more work to do
        $playHand = $game->getPlayHand();
		$playHand = getUniqueCards($playHand);
        if (sizeof($playHand) > 0) {
            $playHand[] = new Option(1, "None");

            $output = MySocket::GetUserReply($game, Input_ChooseOne, $playHand, 1, 1, null, "Choose card to trash");
            $card_name = $output[0];
            if ($card_name != "None") {
                $card = $game->moveCardFromPileToPile($card_name, PlayHand, Trash);
                 ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($card), "trashes " . ScreenDisplay::$LMSG);
            }
        }
        $game->cardActionFinished();
        return true;
    }

}

?>